package xainchenggaoji;

import org.junit.Test;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author Mr.ma
 * @since 2022/11/2 21:16
 * 线程池的创建可以通过Executors工具类创建，
 * 也可以使用较为原始的ThreadPoolExecutor对象进行创建（阿里规范建议使用方式）
 * 线程池有：
 *      newFixedThreadExecutor:固定大小线程池
 *      newSingleThreadPool:单一线程池
 *      newCachedThreadPool:可变线程池
 *      newScheduledThreadPool:延迟线程池
 **/
public class ExecutorTest {
    @Test
    public void TestSingle(){
        TestRunnable testRunnable = new TestRunnable();
        ExecutorService executorService = Executors.newSingleThreadExecutor();
        for (int i = 0; i < 10; i++) {
            executorService.submit(testRunnable);
        }
    }
    @Test
    public void TestFixed(){
        TestRunnable testRunnable = new TestRunnable();
        ExecutorService executorService = Executors.newFixedThreadPool(3);
        for (int i = 0; i < 20; i++) {
            executorService.submit(testRunnable);
        }
    }
}

class TestRunnable implements Runnable{
    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName());
    }
}

